package View;

import Dao.iml.AirInfoDaoImpl;
import bean.AirInfo;

import java.sql.ResultSet;
import java.util.List;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        int n = 0;
        System.out.println("****************欢迎登录航班信息系统*****************");
        do {
            System.out.println("请选择操作:1.查询所有航班 2.按起飞时间查询 3.按目的地查询 4.删除航班 5.更新航班 6.退出");
            Scanner scanner = new Scanner(System.in);
            n = scanner.nextInt();
            AirInfoDaoImpl airInfoDao = new AirInfoDaoImpl();
            switch (n) {
                case 1:
                    List<AirInfo> allInfo = airInfoDao.getAllInfo();
                    System.out.println("\t编号\t航班号\t目的地\t起飞时间");
                    for (AirInfo airInfo : allInfo) {
                        System.out.println("\t" + airInfo.getId() + "\t" + airInfo.getNum() + "\t\t" + airInfo.getAddress() + "\t\t" + airInfo.getDate());
                    }
                    break;
                case 2:
                    System.out.println("请输入起飞时间：");
                    String date = scanner.next();
                    List<AirInfo> Infos = airInfoDao.getByDate(date);
                    System.out.println("\t编号\t航班号\t目的地\t起飞时间");
                    for (AirInfo Info : Infos) {
                        System.out.println("\t" + Info.getId() + "\t" + Info.getNum() + "\t\t" + Info.getAddress() + "\t\t" + Info.getDate());
                    }
                    break;
                case 3:
                    System.out.println("请输入目的地：");
                    String address = scanner.next();
                    List<AirInfo> Infos1 = airInfoDao.getByAddress(address);
                    System.out.println("\t编号\t航班号\t目的地\t起飞时间");
                    for (AirInfo Info : Infos1) {
                        System.out.println("\t" + Info.getId() + "\t" + Info.getNum() + "\t\t" + Info.getAddress() + "\t\t" + Info.getDate());
                    }
                    break;
                case 4:
                    System.out.println("请输入航班号：");
                    String num = scanner.next();
                    ResultSet byNum1 = airInfoDao.findByNum(num);
                    if (byNum1!=null){
                        boolean b = airInfoDao.deleteByNum(num);
                        if (b==true){
                            System.out.println("删除成功！");
                        }else {
                            System.out.println("删除失败！");
                        }
                    }else {
                        System.out.println("航班不存在，请检查航班号！");
                    }

                    break;
                case 5:
                    System.out.println("请输入航班号：");
                    String num1 = scanner.next();
                    ResultSet byNum = airInfoDao.findByNum(num1);
                    if (byNum!=null){
                        System.out.println("请输入新的id：");
                        int newId = scanner.nextInt();
                        System.out.println("请输入新的航班号：");
                        String newNum = scanner.next();
                        System.out.println("请输入新的目的地：");
                        String newAddress = scanner.next();
                        System.out.println("请输入新的起飞时间：");
                        String newDate = scanner.next();

                        AirInfo airInfo = new AirInfo();
                        airInfo.setId(newId);
                        airInfo.setNum(newNum);
                        airInfo.setAddress(newAddress);
                        airInfo.setDate(newDate);
                        airInfo.setNum(num1);
                        boolean b1 = airInfoDao.updateByNum(num1,airInfo);
                        if (b1==true){
                            System.out.println("修改成功！");
                        }else {
                            System.out.println("修改失败！");
                        }
                    }else {
                        System.out.println("航班不存在，请检查航班号！");
                    }

                    break;
                case 6:
                    break;
                default:
                    System.out.println("请输入1-6的数字");
            }
        } while (n!= 6);
        System.out.println("谢谢使用！");
    }
}

